import '@babel/polyfill';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import gsap from 'gsap';
import * as dat from 'dat.gui';

//三维网站

//场景
const scene = new THREE.Scene();

//相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(0, 0, 15);
scene.add(camera);

//渲染器
const renderer = new THREE.WebGL1Renderer({ antialias: true, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

//【投射光线】
const cube = new THREE.BoxGeometry(1, 1, 1);
const material1 = new THREE.MeshBasicMaterial({ wireframe: true });
const material2 = new THREE.MeshBasicMaterial({ color: 0x00ff00 });

const cubeArr = new THREE.Group();
for (let i = 0; i < 5; i++) {
    for (let j = 0; j < 5; j++) {
        for (let k = 0; k < 5; k++) {
            const mesh = new THREE.Mesh(cube, material1);
            mesh.position.set(i, j, k);
            //scene.add(mesh);
            cubeArr.add(mesh);

        }
    }
}
cubeArr.position.set(-2, -2, 0);
scene.add(cubeArr);

const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

//intersectObject检测mesh，intersectObjects检测数组arry
window.addEventListener('dblclick', (event) => {
    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -((event.clientY / window.innerHeight) * 2 - 1);
    raycaster.setFromCamera(mouse, camera);
    const result = raycaster.intersectObject(cubeArr);
    if (result.length > 0) {
        result[1].object.material = material2;
    } else {
        console.log('没有点击到物体');
    }
});


//【炫酷三角形】
const sjxGroup = new THREE.Group();
for (let i = 0; i < 50; i++) {
    //每一个三角形，需要3个顶点，每个顶点需要3个值
    const geometry = new THREE.BufferGeometry();
    const positionArry = new Float32Array(9);
    for (let j = 0; j < 9; j++) {
        if (j % 3 == 1) {
            positionArry[j] = Math.random() * 10 - 5;//y：-5~-5
        } else {
            positionArry[j] = Math.random() * 10 - 5;//x,z：-5~-5
        }
    }
    geometry.setAttribute('position', new THREE.BufferAttribute(positionArry, 3));
    const color = new THREE.Color(Math.random(), Math.random(), Math.random());
    const material = new THREE.MeshBasicMaterial({
        color: color,
        opacity: 0.5,
        transparent: true,
        side: THREE.DoubleSide
    });
    const mesh = new THREE.Mesh(geometry, material);
    sjxGroup.add(mesh);
}
sjxGroup.position.set(0, -35, 0);//将组的y轴坐标系整个进行更改
scene.add(sjxGroup);

//【点光源小球】
const ballGroup = new THREE.Group();
const sphereGeometry = new THREE.SphereGeometry(1, 50, 50);
const Material = new THREE.MeshStandardMaterial({ color: 0xb3b3b3 });
const sphere = new THREE.Mesh(sphereGeometry, Material);
ballGroup.add(sphere);
const planeGeometry = new THREE.PlaneGeometry(50, 50);
const plane = new THREE.Mesh(planeGeometry, Material);
plane.position.set(0, -1, 0);
plane.rotateX(-Math.PI / 2);
ballGroup.add(plane);
const ball = new THREE.Mesh(new THREE.SphereGeometry(0.1, 20, 20), new THREE.MeshBasicMaterial({ color: 0xff0000 }));
//ball.position.set(0, 3, 3);
ballGroup.add(ball);
//创建光
const light = new THREE.AmbientLight(0xf2f2f2, 0.3);//环境光，无阴影
const pointLight = new THREE.PointLight(0xff0000, 0.5);//点光源
pointLight.position.set(8, 8, 8);
ball.add(pointLight);
ballGroup.add(light);
//产生阴影
renderer.shadowMap.enabled = true;
sphere.castShadow = true;
plane.receiveShadow = true;
pointLight.castShadow = true;
scene.add(ballGroup);
ballGroup.position.set(0, -70, 0);

//模型成组
const meshArry = [cubeArr, sjxGroup, ballGroup];

const mouse1 = new THREE.Vector2();
window.addEventListener('mousemove', (event) => {
    mouse1.x = (event.clientX / window.innerWidth) - 0.5;
})

//帧动画
const clock = new THREE.Clock();
function animate() {
    const time = clock.getElapsedTime();
    cubeArr.rotation.x = time * 0.3;
    cubeArr.rotation.y = time * 0.3;

    sjxGroup.rotation.x = time * 0.3;

    ball.position.x = Math.sin(time) * 5;
    ball.position.z = Math.cos(time) * 5;
    ball.position.y = Math.sin(time * 3) * 2 + 1;
    ballGroup.rotation.x = Math.sin(time) * 0.1;

    camera.position.y = -(window.scrollY / window.innerHeight) * 35
    //camera.position.x = mouse1.x * 10;
    camera.position.x += (mouse1.x * 20 - camera.position.x) * 0.03;

    renderer.render(scene, camera);
    requestAnimationFrame(animate);
};
animate();

//窗口调整侦听
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
});

let currentPage = 0;
window.addEventListener('scroll', () => {
    const newPage = Math.round(window.scrollY / window.innerHeight);
    console.log(newPage);

    if (newPage != currentPage) {
        currentPage = newPage;
        gsap.to(meshArry[currentPage].rotation, {
            y: '+=' + Math.PI,
            duration: 1
        });
        gsap.fromTo(`.page${currentPage} h1`, { x: -500 }, { x: 0, rotate: '+=' + 360, duration: 1 });
    }
})
